package com.qkjia.day02;

import com.qkjia.day01.SleepUtils;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Test04_ReadWriteLock {

    private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
    private ReentrantReadWriteLock.ReadLock readLock = rwLock.readLock();
    private ReentrantReadWriteLock.WriteLock writeLock = rwLock.writeLock();

    public void read() {
        readLock.lock();
        String threadName = Thread.currentThread().getName();
        System.out.println("当前线程:" + threadName + "读进入...");
        SleepUtils.second(3);
        System.out.println("当前线程:" + threadName + "读退出...");
        readLock.unlock();
    }

    public void write() {
        writeLock.lock();
        String name = Thread.currentThread().getName();
        System.out.println("当前线程:" + name + "写进入...");
        SleepUtils.second(3);
        System.out.println("当前线程:" + name + "写退出...");
        writeLock.unlock();
    }

    public static void main(String[] args) {

        final Test04_ReadWriteLock rwl = new Test04_ReadWriteLock();

        Thread t1 = new Thread(() -> {
            rwl.read();
        }, "t1");
        Thread t2 = new Thread(() -> {
            rwl.read();
        }, "t2");

        Thread t3 = new Thread(() -> {
            rwl.write();
        }, "t3");

        Thread t4 = new Thread(() -> {
            rwl.write();
        }, "t4");

//        t1.start();//R
//        t2.start();//R
        //
//		 t1.start(); // R
//		 t3.start(); // W

		t3.start();// W
		t4.start();// W
    }
}